package com.coke.demo.other;

import com.coke.demo.model.Record;
import com.coke.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 19602
 */
@Component
@EnableScheduling
public class ScheduleService {
    /**
     * 设置日期格式
     */
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    private RecordService recordService;

    @Autowired
    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }

    /**
     * 每天23点06分调用
     *
     * @throws ParseException
     */
    @Scheduled(cron = "0 10 23 * * ?")
    private void updateRecordState() throws ParseException {
        List<Record> orders = recordService.findSomeBookTime();
        if (orders != null) {
            for (Record record : orders) {
                String[] time = record.getBookTime().split(" ");
                // new Date()为获取当前系统时间
                Date nd = df.parse(df.format(new Date()));
                Date bt = df.parse(time[0]);
                if (nd.compareTo(bt) > 0 || nd.compareTo(bt) == 0) {
                    System.out.println(record.getRecordid());
                    recordService.updateStateById(record.getRecordid(), "已结束");
                }
            }
        }
    }
}
